package com.zzptc.sky.baidudemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.github.lzyzsd.randomcolor.RandomColor;
import com.zzptc.sky.baidudemo.R;
import com.zzptc.sky.baidudemo.utils.MyApplication;
import com.zzptc.sky.baidudemo.views.CircleTextImageView;
import com.zzptc.sky.baidudemo.vo.ContactsInfo;
import com.zzptc.sky.baidudemo.vo.ContactsInfo2;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/6/6.
 */

public class SOSForSelectShowContactsAdapter extends RecyclerView.Adapter<SOSForSelectShowContactsAdapter.MyHoldView> {

    private List<ContactsInfo> list;

    public SOSForSelectShowContactsAdapter(List<ContactsInfo> list) {
        this.list = list;
    }

    @Override
    public MyHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_sos_selectcontacts,parent,false);
        return new MyHoldView(view);
    }

    @Override
    public void onBindViewHolder(MyHoldView holder, int position) {
        ContactsInfo info = list.get(position);
        holder.ctiv.setText(info.getName().substring(0,1));
        holder.ctiv.setFillColor(new RandomColor().randomColor());
        holder.tv_name.setText(info.getName());
        holder.tv_phoneNumber.setText(info.getPhone());
        holder.tv_address.setText(info.getAddr());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHoldView extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView tv_name;
        private TextView tv_phoneNumber;
        private TextView tv_address;
        private CircleTextImageView ctiv;
        private CheckBox cb_select;

        private int count;

        public MyHoldView(View itemView) {
            super(itemView);

            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_phoneNumber = (TextView) itemView.findViewById(R.id.tv_phoneNumber);
            tv_address = (TextView) itemView.findViewById(R.id.tv_addres);
            ctiv = (CircleTextImageView) itemView.findViewById(R.id.ctiv);
            cb_select = (CheckBox) itemView.findViewById(R.id.cb_select);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            ContactsInfo c = list.get(position);
            String tel = c.getPhone().replace(" ","").replace("-","");
            if (tel != null && !"".equals(tel) && tel.matches("^1[3578]\\d{9}$")){
                //设置多选框的真假，添加计数，选中不能超过3个
                if (cb_select.isChecked()){
                    cb_select.setChecked(false);
                    count--;
                }else {
                    if (count<3){
                        cb_select.setChecked(true);
                        count++;
                    }else {
                        Toast.makeText(MyApplication.getContext(), "不能超过3个", Toast.LENGTH_SHORT).show();
                    }
                }

            }else {
                Toast.makeText(MyApplication.getContext(), "请选中手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
